package com.sky.service.impl;


import com.sky.dto.GoodsSalesDTO;
import com.sky.entity.Orders;
import com.sky.mapper.ReportMapper;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private ReportMapper reportMapper;

    /**
     * 营业额统计
     * @param begin
     * @param end
     * @return
     */
    @Override
    public TurnoverReportVO turnoverStatistics(LocalDate begin, LocalDate end) {
       List<LocalDate> localDateList=new ArrayList<>();
       localDateList.add(begin);

       while (!begin.equals(end)) {
           begin=begin.plusDays(1);
           localDateList.add(begin);
       }
//       日期的整理
       String dateList= StringUtils.join(localDateList,",");
//       营业额
       List<Double> turnoverList=new ArrayList<>();
       localDateList.forEach(localDate ->  {
           LocalDateTime beginTime = LocalDateTime.of(localDate, LocalTime.MIN);
           LocalDateTime endTime = LocalDateTime.of(localDate, LocalTime.MAX);
           Map map=new HashMap<>();
           map.put("beginTime",beginTime);
           map.put("endTime",endTime);
           map.put("status", Orders.COMPLETED);
           Double aDouble=reportMapper.finOneMoney(map);
           aDouble= aDouble==null ? 0.0:aDouble;
           turnoverList.add(aDouble);
        });
        String join = StringUtils.join(turnoverList, ",");

        return TurnoverReportVO.builder()
                .dateList(dateList)
                .turnoverList(join)
                .build();

    }

    /**
     * 用户统计
     * @param begin
     * @param end
     * @return
     */
    @Override
    public UserReportVO userStatistics(LocalDate begin, LocalDate end) {
//        得到日期
        List<LocalDate> localDateList=new ArrayList<>();
        localDateList.add(begin);
        while (!begin.equals(end)) {
            begin=begin.plusDays(1);
            localDateList.add(begin);
        }
        String dataList = StringUtils.join(localDateList, ",");
//        新增
        List<Integer>  newUserList=new ArrayList<>();
        localDateList.forEach(localDate ->  {
            LocalDateTime begintime = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime endtime = LocalDateTime.of(localDate, LocalTime.MAX);
            Integer oneDayUser=reportMapper.findSumUser(begintime,endtime);
            oneDayUser= oneDayUser==null? 0:oneDayUser;
            newUserList.add(oneDayUser);
        });
        String newuserList = StringUtils.join(newUserList, ",");
//        总用户
        List<Integer>  totalUserList=new ArrayList<>();
        localDateList.forEach(localDate ->  {
            LocalDateTime endtimes = LocalDateTime.of(localDate, LocalTime.MAX);
            Integer sumUser = reportMapper.findSumUser(null, endtimes);
            sumUser =sumUser==null? 0:sumUser;
            totalUserList.add(sumUser);
        });
        String totaluserList = StringUtils.join(totalUserList, ",");

        return  UserReportVO.builder()
                .dateList(dataList)
                .newUserList(newuserList)
                .totalUserList(totaluserList)
                .build();

    }

    /**
     * 订单统计
     * @param begin
     * @param end
     * @return
     */
    @Override
    public OrderReportVO ordersStatistics(LocalDate begin, LocalDate end) {
//        日期
        List<LocalDate> localDateList=new ArrayList<>();
        localDateList.add(begin);
        while(!begin.equals(end)) {
            begin=begin.plusDays(1);
            localDateList.add(begin);
        }
        String Datelist = StringUtils.join(localDateList, ",");
//        每天订单列表
        List<Integer> orderOne=new ArrayList<>();
//        有效订单列表
        List<Integer> completeOrder=new ArrayList<>();
        localDateList.forEach(localDate ->  {
            LocalDateTime begintime = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime endtime = LocalDateTime.of(localDate, LocalTime.MAX);
            Integer everyOrder = reportMapper.findEveryOrder(begintime, endtime, null);
            Integer complete = reportMapper.findEveryOrder(begintime, endtime, Orders.COMPLETED);
            everyOrder = everyOrder==null? 0:everyOrder;
            complete = complete==null? 0:complete;
            orderOne.add(everyOrder);
            completeOrder.add(complete);
        });
//        总订单
        Integer sumOrder = orderOne.stream().reduce(Integer::sum).get();
//        有效订单总数
        Integer everyOrder = completeOrder.stream().reduce(Integer::sum).get();
//        订单完成率
        Double  orderCompleteRate=0.0;
        if(sumOrder!=0) {
           orderCompleteRate =everyOrder.doubleValue() /  sumOrder;
        }
        return OrderReportVO.builder()
                .dateList(Datelist)
                .orderCompletionRate(orderCompleteRate)
                .validOrderCountList(StringUtils.join(completeOrder,","))
                .totalOrderCount(sumOrder)
                .orderCountList(StringUtils.join(orderOne,","))
                .validOrderCount(everyOrder)
                .build();

    }

    /**
     * TOP 10
     * @param begin
     * @param end
     * @return
     */
    @Override
    public SalesTop10ReportVO top10(LocalDate begin, LocalDate end) {
        List<GoodsSalesDTO> goodsSalesDTOS=reportMapper.top10(LocalDateTime.of(begin,LocalTime.MIN),LocalDateTime.of(end,LocalTime.MAX),Orders.COMPLETED);
//        销量
        List<Integer> number=new ArrayList<>();
//        商品名称
        List<String> name=new ArrayList<>();
//        学习一下这个方法 快速生成 List
//        List<String> collect = goodsSalesDTOS.stream().map(GoodsSalesDTO::getName).collect(Collectors.toList());
        goodsSalesDTOS.forEach(goodsSalesDTO ->  {
            name.add(goodsSalesDTO.getName());
            number.add(goodsSalesDTO.getNumber());
        });
        return SalesTop10ReportVO.builder()
                .nameList(StringUtils.join(name,","))
                .numberList(StringUtils.join(number,","))
                .build();
    }
}
